package com.gable.exhibition.init;

import com.alibaba.fastjson.JSON;
import com.gable.exhibition.dao.ItemDao;
import com.gable.exhibition.model.Item;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lesson on 2018/3/26.
 */
@Component
public class ItemInIt {

        public static Map<Long,Item> itemMapByItemId;

        public static Map<String,List<Item>> itemMapByType;

        Logger logger = Logger.getLogger(this.getClass());

        @Autowired
        ItemDao itemDao;

        public void itemMapInit(){
            if(itemMapByItemId == null){
                itemMapByItemId = new HashMap<Long,Item>();
            }

            if(itemMapByType == null){
                itemMapByType =  new HashMap<String,List<Item>>();
            }
            List<Item> itemList = itemDao.queryItem(null);

            String type = "";
            List<Item> itemList1 = null;
            for(Item item : itemList){
                if(type.equals("") || !type.equals(item.getType())){
                    itemList1 = new ArrayList<Item>();
                    itemList1.add(item);
                    type = item.getType();
                    itemMapByType.put(type,itemList1);
                }else{
                    itemList1.add(item);
                }
                itemMapByItemId.put(item.getItemId(),item);
            }

            logger.debug("itemMapByType ========== " + JSON.toJSONString(itemMapByType));
            logger.debug("初始化字典表结束");
        }
}
